package com.reservation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reservation.common.Result;
import com.reservation.entity.Announcement;
import com.reservation.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 公告控制器
 */
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 添加公告（仅管理员可用）
     *
     * @param announcement 公告信息
     * @return 添加结果
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Announcement> addAnnouncement(@RequestBody @Valid Announcement announcement) {
        Announcement addedAnnouncement = announcementService.addAnnouncement(announcement);
        return Result.success(addedAnnouncement, "添加成功");
    }

    /**
     * 修改公告信息（仅管理员可用）
     *
     * @param announcement 公告信息
     * @return 修改结果
     */
    @PutMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Announcement> updateAnnouncement(@RequestBody @Valid Announcement announcement) {
        Announcement updatedAnnouncement = announcementService.updateAnnouncement(announcement);
        return Result.success(updatedAnnouncement, "修改成功");
    }

    @PutMapping("/publish/{id}")
    public Result<String> updateStatus(@PathVariable Long id) {
        boolean result = announcementService.updateStatus(id);

        if (result) {
            return Result.success("公告发布成功");
        } else {
            return Result.success("公告发布失败");
        }
    }


    /**
     * 获取公告详情
     *
     * @param id 公告ID
     * @return 公告信息
     */
    @GetMapping("/{id}")
    public Result<Announcement> getAnnouncementById(@PathVariable Long id) {
        Announcement announcement = announcementService.getAnnouncementById(id);
        return Result.success(announcement);
    }

    /**
     * 删除公告（仅管理员可用）
     *
     * @param id 公告ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> deleteAnnouncement(@PathVariable Long id) {
        boolean result = announcementService.deleteAnnouncement(id);
        return Result.success(null, "删除成功");
    }

    /**
     * 分页查询公告列表
     *
     * @param current 当前页
     * @param size    每页大小
     * @param title   公告标题（可选）
     * @return 公告列表
     */
    @GetMapping("/list")
    public Result<IPage<Announcement>> getAnnouncementList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String status){
        Page<Announcement> page = new Page<>(pageNum, pageSize);
        IPage<Announcement> announcementPage = announcementService.getAnnouncementList(page, title, status);
        return Result.success(announcementPage);
    }

    /**
     * 获取所有公告
     *
     * @return 公告列表
     */
    @GetMapping("/all")
    public Result<List<Announcement>> getAllAnnouncements() {
        List<Announcement> announcements = announcementService.getAllAnnouncements();
        return Result.success(announcements);
    }

    /**
     * 获取最新的公告
     *
     * @param limit 限制数量
     * @return 公告列表
     */
    @GetMapping("/latest")
    public Result<List<Announcement>> getLatestAnnouncements(
            @RequestParam(defaultValue = "5") Integer limit) {
        List<Announcement> announcements = announcementService.getLatestAnnouncements(limit);
        return Result.success(announcements);
    }
}